const app = getApp()
const adapters = require("../../utils/adapters");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    header: {
      title: '诺力锂电',
      whiteBg: false,
      mode: 'title',
      navBarHeight: app.globalData.navBarHeight,
      tabbarHeight: app.globalData.tabbarHeight,
      screenHeight: app.globalData.screenHeight,
      screenWidth: app.globalData.screenWidth,
    },
    user: {
      phoneNumber: '',
      password: '',
      remeber: true
    }
  },

  onPageScroll(e) {
    this.setData({
      'header.whiteBg': e.scrollTop < 130 ? false : true
    })
  },

  onLoad(options) {
    const that = this
    wx.getStorage({
      key: "user",
      success(res) {
        console.log("获取的数据为", res.data);
        that.setData({user: res.data})
      },
      fail(err) {
        console.error("读取数据失败", err);
      }
    })
  },

  navigatePage(e) {
    const pageName = e.currentTarget.dataset.pagename
    wx.navigateTo({
      url: '/pages/' + pageName + '/' + pageName,
    })
  },

  phoneNumberInput(e) {
    const phoneNumber = e.detail.value
    this.setData({
      "user.phoneNumber": phoneNumber
    })
  },

  passwordInput(e) {
    const password = e.detail.value
    this.setData({
      "user.password": password
    })
  },

  remeberUser() {
    this.setData({
      "user.remeber": !this.data.user.remeber
    })
  },

  // 网络请求 登录
  async login() {
    const user = this.data.user
    if(user.phoneNumber === '') {
      wx.showToast({
        title: '请填写账号！',
        icon: 'none'
      })
      return
    }
    const loginRes = await adapters.login(user)
    if(loginRes.success) {
      wx.navigateTo({
        url: '/pages/index/index',
      })
    }else {
      wx.showToast({
        title: '登录失败:' + loginRes.msg,
        icon: 'none'
      })
    }
  },
})